package com.xuecheng.search.controller;

import com.xuecheng.commons.model.dto.CoursePubIndexDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.search.service.SearchService;
import com.xuecheng.web.exceptions.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 学生端Controller
 *
 * @author 七安
 */
@RestController
@RequestMapping("/course-search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 搜索课程
     *
     * @param coursePubIndexDto 搜索条件
     * @param pageRequest       分页数据
     * @return 结果
     */
    @PostMapping
    public PageResponseResult findCourseByEs(@RequestBody CoursePubIndexDto coursePubIndexDto, PageRequest pageRequest) throws IOException {
        return searchService.findCourseByEs(coursePubIndexDto, pageRequest);
    }

    /**
     * 根据id从Es索引库中查询
     *
     * @param id id
     * @return 结果
     */
    @GetMapping("/{id}")
    public ResponseResult findCourseById(@PathVariable("id") Long id) throws BusinessException, IOException {
        return searchService.findCourseById(id);
    }
}
